
package com.duobao.view;
import java.awt.*;

import javax.swing.*;

import com.duobao.game.DuobaoSwingLaunch;
import com.duobao.server.*;
import com.duobao.uitl.*;

import java.awt.event.*;
import java.lang.reflect.*;

public class Xueyi extends JPanel implements MouseListener{
    DuobaoSwingLaunch game;
    Map heroMap;
    NPC npc;
    JLabel fanhui;
    JLabel fanhuiyouxi;
    WuGong wugon1;
    WuGong wugon2;

    JLabel tishi;
    JLabel shenjitishi;
    JLabel jishentishi;
    public Xueyi(DuobaoSwingLaunch game, NPC npc, WuGong wg)
    {
        this.game=game;
        this.heroMap=Maps.getMap(this.game.hero.getLocation());
        GridLayout gl=new GridLayout(16,1,1,1);
        this.setLayout(gl);
        this.wugon2=wg;
        this.npc=npc;
        xiaoguo();

        setFanhui();
        setFanhuiyouxi();
    }
    private void xiaoguo()
    {
        String name=this.wugon2.getName();
        this.wugon1=this.game.hero.getWg(name);//获取自己的武功


        if(this.wugon1==null)
        {
            try {
                Constructor con=this.wugon2.getClass().getDeclaredConstructor(int.class);
                this.wugon1=(WuGong) con.newInstance(1);
                this.game.hero.getWGs().add(wugon1);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            setShenjitishi();
        }else if(this.wugon1.getLv()>=this.wugon2.getLv())
        {
            setchushitishi();
        }else if(!ifxiuwei(this.wugon1.getLv()))
        {
            setxiuweitishi();
        }else
        {
            if(this.game.hero.getProficiency()==0)
            {
                setqiannengtishi();
            }else{
                float f=((float)this.game.hero.getSP())/((float)this.game.hero.getMaxSP());
                if(f<=0.1f)
                {
                    setJishentishi();
                }else
                {
                    if(this.game.hero.xueyi_baishiIf(this.wugon1))
                    {
                        this.game.hero.xueyi_Baishi(this.wugon1);
                        setShenjitishi();
                    }else
                    {
                        this.game.hero.xueyi_Baishi(this.wugon1);

                        setTishi();
                    }

                }
            }

        }
    }
    private boolean ifxiuwei(int lv)//修为是否可以学习
    {

        //System.out.println(lv);
        Float xiuwei=lv*lv*lv/1000.0f;
        //System.out.println(xiuwei);
        int nian=xiuwei.intValue();
        Float tianf=((xiuwei-nian)*250);
        int tian=tianf.intValue();
        Float shic=(tianf-tian)*12;
        int sc=shic.intValue();
        //System.out.println("nian:"+nian+"    tian:"+tian+"    sc:"+sc);
        if(this.game.hero.getCultivationN()<nian)
        {
            return false;
        }else if(this.game.hero.getCultivationY()<tian)
        {
            return false;
        }else if(this.game.hero.getCultivationR()<sc)
        {
            return false;
        }
        return true;
    }
    private void setTishi() {
        tishi=new JLabel("你虚心的向"+this.npc.getName()+"请教，你的"+this.wugon1.getName()+"视乎有些进步("+this.wugon1.getLv()+"级/"+this.wugon1.getEndurance()+"%)");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setShenjitishi() {
         String result = Zuanhuan.zhuang(this.wugon1.getLv());

        tishi=new JLabel(this.wugon1.getName()+"等级提高到"+result+"级");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setJishentishi() {
        tishi=new JLabel("你看起来昏昏欲睡，快去休息吧");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setxiuweitishi() {
        tishi=new JLabel("你的修为不够，你是否还在修行上下功夫呢？");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setqiannengtishi() {
        tishi=new JLabel("你的潜能耗尽了，快去找人切磋切磋吧");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setchushitishi() {
        tishi=new JLabel("你的"+this.wugon2.getName()+"武功已经超出为师了,请另寻名师吧");
        tishi.setFont(myFont.f2);
        tishi.addMouseListener(this);
        JPanel jp1=new JPanel();
        jp1.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp1.add(tishi);
        this.add(jp1);
    }
    private void setFanhui() {
        fanhui=new JLabel();
        fanhui.addMouseListener(this);
        fanhui.setFont(myFont.f2);
        fanhui.setForeground(Color.BLUE);
        fanhui.setText("<html><u>"+"返回"+"</u></html>");
        JPanel jp3=new JPanel();
        jp3.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp3.add(fanhui);
        this.add(jp3);
    }

    private void setFanhuiyouxi() {
        fanhuiyouxi=new JLabel();
        fanhuiyouxi.addMouseListener(this);
        fanhuiyouxi.setFont(myFont.f2);
        fanhuiyouxi.setForeground(Color.BLUE);
        fanhuiyouxi.setText("<html><u>"+"返回游戏"+"</u></html>");
        JPanel jp4=new JPanel();
        jp4.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
        jp4.add(fanhuiyouxi);
        this.add(jp4);
    }
    private boolean iFText(JLabel jl)
    {
        boolean f=false;
        if(jl==tishi)
        {
            f=true;
        }
        return f;
    }

    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==fanhuiyouxi)
        {
            this.game.setPanel(new MainPanel(this.game,1));
        }else if(e.getSource()==fanhui)
        {
            try {
                this.game.setPanel(new Xueyiwugong(this.game,this.npc,1));
            }catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        if(iFText((JLabel)e.getSource()))
        {
            ((JLabel)e.getSource()).setCursor(new Cursor(Cursor.TEXT_CURSOR));
        }
    }
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}
